package DAO;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import DBUntil.DbHelper;
import each_class.User;

public class L_likeDAO {
	private static DbHelper db = new DbHelper();

	// 判断歌曲或歌单或专辑是否存在
	public static boolean judgeExist(String id) throws IOException {
		String sql = "select user_id from I_Like where list_id=? or song_id=? or album_id=?";
		Map<String, Object> map = db.findsingle(sql, id, id, id);
		if (null == map) {
			return true;
		}
		return false;
	}

	// 添加歌曲
	public static int addI_Like_song(String user_id, String song_id) throws Exception {
		if (judgeExist(song_id)) {
			String sql = "insert into I_Like values(seq_I_Like_id.nextval,?,null,null,?)";
			return db.update(sql, user_id, song_id);
		} else {
			return 0;
		}
	}

	// 添加歌单
	public static int addI_Like_list(String user_id, String list_id) throws Exception {
		if (judgeExist(list_id)) {
			String sql = "insert into I_Like values(seq_I_Like_id.nextval,?,?,null,null)";
			return db.update(sql, user_id, list_id);
		} else {
			return 0;
		}
	}

	// 添加专辑
	public static int addI_Like_album(String user_id, String album_id) throws Exception {
		if (judgeExist(album_id)) {
			String sql = "insert into I_Like values(seq_I_Like_id.nextval,?,?,null,?,null)";
			return db.update(sql, user_id, album_id);
		} else {
			return 0;
		}
	}

	// 查找歌曲
	public static List<Map<String, Object>> findsong(String id) throws IOException {
		String sql = "select song_id from I_Like where user_id=?";
		List<Map<String, Object>> lmap = db.findMutip(sql, id);
		return lmap;
	}
	
	// 查找一首歌曲
	public static Map<String, Object> findsong(String user_id,String id) throws IOException {
		String sql = "select song_id from I_Like where user_id=? and song_id=?";
		Map<String, Object> map = db.findsingle(sql, user_id,id);
		return map;
	}
	

	// 查找歌单
	public static List<Map<String, Object>> findList(String id) throws IOException {
		String sql = "select list_id from I_Like where user_id=?";
		List<Map<String, Object>> lmap = db.findMutip(sql, id);
		return lmap;
	}

	// 查找专辑
	public static List<Map<String, Object>> findalbum(String id) throws IOException {
		String sql = "select album_id from I_Like where user_id=?";
		List<Map<String, Object>> lmap = db.findMutip(sql, id);
		return lmap;
	}

	// 删除歌单
	public static int disListLike(String uid, String id) throws Exception {
		String sql = "delete from I_Like where user_id=? and list_id=? ";
		return db.update(sql, uid, id);
	}

	// 删除歌曲
	public static int dissongLike(String uid, String id) throws Exception {
		String sql = "delete from I_Like where user_id=? and song_id=? ";
		return db.update(sql, uid, id);
	}

	// 删除专辑
	public static int disalbum(String uid, String id) throws Exception {
		String sql = "delete from I_Like where user_id=? and user_id=? ";
		return db.update(sql, uid, id);
	}

}